<!--
 * @Descripttion: 设置页面
 * @version: 
 * @Author:Hui
 * @Date: 2023-05-11 11:36:57
 * @LastEditTime: 2025-09-07 18:33:40
 * @LastEditors: sueRimn
-->

<template>
  <view class="setting">
    <!-- #ifdef H5 -->
    <custom-navbar title="设置" />
    <!-- #endif -->
    <setting-item :list="list" @handleItem="handleItem" />
    <button class="logout" @click="handleLogout">退出登录</button>
    <copy-right :version="version" :envVersion="envVersion" :year="year" @handleChangeEnv="handleChangeEnv" />
  </view>
</template>


<script>

import { mapGetters } from 'vuex'
import { fetchSettingList } from '@/api'
import SettingItem from './components/settingItem.vue'
import CopyRight from './components/copyRight'
import { urlJump,clearExpList } from '@/utils'
export default {
  name: 'Setting',
  computed: {
    ...mapGetters([
      'userInfo'
    ]),
  },
  components: {
    SettingItem, CopyRight
  },
  data() {
    return {
      list: [],
      version: '',
      envVersion: '',
      year: 0,
      phone: ""
    }
  },
  mounted() {
    this.getAppData();
    this.getSettingList();
  },
  methods: {
    //获取设置列表
    async getSettingList() {
      //加载中
      uni.showLoading({ title: '加载中' });
      const result = await fetchSettingList({});
      console.log('设置列表', result.data);
      uni.hideLoading();
      if (result.code == 200) {
        this.list = result.data;
      }
    },
    getAppData() {
      let miniProgram = { "appId": "1001", "version": "1.0.0", envVersion: "h5" }
      // #ifndef H5
      let accountInfo = uni.getAccountInfoSync();
      miniProgram = accountInfo.miniProgram
      // #endif
      let { appId, version, envVersion } = miniProgram

      const year = new Date().getFullYear();
      version = version ? version : "1.0.0"
      this.version = version;
      this.envVersion = envVersion;
      this.year = year;

      // for (let i = 0; i < this.list.length; i++) {
      //   const item = this.list[i];
      //   if (item.text == "检查版本") {
      //     item.value = version;
      //   }
      // }
    },
    handleLogout() {
      uni.showModal({
        title: '',
        content: '是否退出登录',
        cancelText: '取消',
        confirmText: '退出登录',
        success: res => {
          if (res.confirm) {
            this.$store.dispatch('user/loadLogout').then(res => {
              if (res) {
                console.log('退出登录成功',res);
                // uni.switchTab({ url: '/pages/user/index' })
                uni.navigateBack();
              }
            })
          }
        }
      })
    },
    //点击
    handleItem(item) {
      if (item.type == 2) {
        const _item = { routeType: 1, target: item.link }
        urlJump(_item)
      } else if (item.type == 3) {
        urlJump({ routeType: 2, target: item.link })
      } else if (item.type == 4) { //切换环境
        this.handleChangeEnv();
      } else if (item.type == 5) { //清除缓存
        this.handleClearCache();
      }
    },
    //切换环境
    handleChangeEnv() {
      uni.showActionSheet({
        itemList: ['修改服务器地址', '修改token'],
        success: (res) => {
          if (res.tapIndex === 0) {
            // 修改服务器地址
            this.handleChangeHost();
          } else if (res.tapIndex === 1) {
            // 修改token
            this.handleChangeToken();
          }
        }
      });
    },
    //修改服务器地址
    handleChangeHost() {
      const currentHost = uni.getStorageSync('setting_host') || '';
      console.log(currentHost)
      // #ifdef MP-ALIPAY
      my.prompt({
        title: '请填写服务器地址',
        message: '',
        placeholder: currentHost,
        defaultValue: currentHost,
        okButtonText: '确认',
        cancelButtonText: '取消',
        success: (res) => {
          if (res.ok) {
            console.log('输入的服务器地址：', res.inputValue);
            uni.setStorageSync('setting_host', res.inputValue);
            uni.showToast({
              title: '服务器地址已更新',
              icon: 'success'
            });
          }
        }
      });
      // #endif
      // #ifndef MP-ALIPAY
      uni.showModal({
        title: '请填写服务器地址',
        content: currentHost,
        editable: true,
        placeholderText: currentHost,
        confirmText: '确认',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            console.log('输入的服务器地址：', res.content);
            uni.setStorageSync('setting_host', res.content);
            uni.showToast({
              title: '服务器地址已更新',
              icon: 'success'
            });
          }
        }
      });
      // #endif
    },
    //修改token
    handleChangeToken() {
      // #ifdef MP-ALIPAY
      my.prompt({
        title: '请填写token',
        message: '',
        placeholder: '请填写token',
        okButtonText: '确认',
        cancelButtonText: '取消',
        success: (res) => {
          if (res.ok) {
            console.log('输入的token：', res.inputValue);
            uni.setStorageSync('token', res.inputValue);
            uni.showToast({
              title: 'Token已更新',
              icon: 'success'
            });
          }
        }
      });
      // #endif
      // #ifndef MP-ALIPAY
      uni.showModal({
        title: '请填写token',
        content: '',
        editable: true,
        placeholderText: '请填写token',
        confirmText: '确认',
        cancelText: '取消',
        success: (res) => {
          if (res.confirm) {
            console.log('输入的token：', res.content);
            uni.setStorageSync('token', res.content);
            uni.showToast({
              title: 'Token已更新',
              icon: 'success'
            });
          }
        }
      });
      // #endif
    },
    //清除缓存
    handleClearCache() {
      uni.showModal({
        title: '清除缓存',
        content: '是否清除缓存',
        cancelText: '取消',
        confirmText: '清除缓存',
        success: res => {
          if (res.confirm) {
            clearExpList();
          }
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 30rpx;
  background-color: #f2f3f7;
  min-height: 100vh;
  /* #ifdef H5 */
  padding-top: calc(var(--status-bar-height) + 88rpx);
  /* #endif */

  .logout {
    border-radius: 10rpx;
    line-height: 90rpx;
    margin: 100rpx auto 0;
    background-color: #fff;
    width: 100%;
  }
}
</style>